package orchestration.android.execution;

import java.util.HashMap;
import java.util.Set;

import orchestration.android.parcelable.ParcelableAndroidTaskDTO;
import orchestration.android.parcelable.ParcelableAnnotation;
import orchestration.android.parcelable.ParcelableProperty;
import orchestration.businessprocess.execution.BPExecution;
import orchestration.businessprocess.model.BPArtifact;
import orchestration.businessprocess.model.nodes.BPNode;

public class DTOFactory {

	@SuppressWarnings("unchecked")
	public static ParcelableAndroidTaskDTO createDTO(BPNode node, BPExecution execution) {
		ParcelableAndroidTaskDTO dto= new ParcelableAndroidTaskDTO();
		dto.setTaskId(node.getId().getValue());
		dto.setTaskName(node.getName());
		HashMap<String, ParcelableProperty> inputProperties= DTOFactory.getProperties(node.getInput(), execution);
		HashMap<String, ParcelableProperty> outputProperties= DTOFactory.getProperties(node.getOutput(), execution);
		
		dto.setInputProperties(inputProperties);
		dto.setOutputProperties(outputProperties);
		dto.setAnnotations((HashMap<String, ParcelableAnnotation>)node.getAttachedAnnotations());
		return dto;
	}

	private static HashMap<String,ParcelableProperty> getProperties(Set<BPArtifact> artifacts,BPExecution execution) {
		HashMap<String, ParcelableProperty> properties= new HashMap<String, ParcelableProperty>();
		if(artifacts!=null)
			for(BPArtifact art : artifacts){
				String name=art.getName();
				properties.put(name,(ParcelableProperty) execution.getProperty(name));
			}
		return properties;
	}

}